package com.htg.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;

import java.io.Serializable;
import java.util.List;

public class UtilNative {

    static Paint paint;
    static {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

    public static void drawBitmap(Bitmap bmp, List<Point> pts) {
        Canvas c = new Canvas(bmp);
        drawCanvas(c, pts);
    }



    public static class Point  implements Serializable {
        public float x;
        public float y;
        float dx, dy;
    }

    public static void drawCanvas(Canvas canvas, List<Point> pts){
        if (pts.size() > 1){
            Path path = new Path();
            final int SMOOTH_VAL = 6;
            for(int i = pts.size() - 2; i < pts.size(); i++){
                if(i >= 0){
                    Point point = pts.get(i);

                    if(i == 0){
                        Point next = pts.get(i + 1);
                        point.dx = ((next.x - point.x) / SMOOTH_VAL);
                        point.dy = ((next.y - point.y) / SMOOTH_VAL);
                    }
                    else if(i == pts.size() - 1){
                        Point prev = pts.get(i - 1);
                        point.dx = ((point.x - prev.x) / SMOOTH_VAL);
                        point.dy = ((point.y - prev.y) / SMOOTH_VAL);
                    }
                    else{
                        Point next = pts.get(i + 1);
                        Point prev = pts.get(i - 1);
                        point.dx = ((next.x - prev.x) / SMOOTH_VAL);
                        point.dy = ((next.y - prev.y) / SMOOTH_VAL);
                    }
                }
            }

            boolean first = true;
            for(int i = 0; i < pts.size(); i++){
                Point point = pts.get(i);
                if(first){
                    first = false;
                    path.moveTo(point.x, point.y);
                }
                else{
                    Point prev = pts.get(i - 1);
                    path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x, point.y);
                }
            }
            canvas.drawPath(path, paint);
        } else {
            if (pts.size() == 1) {
                Point point = pts.get(0);
                canvas.drawCircle(point.x, point.y, 2, paint);
            }
        }
    }
}
